package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.dao.AccountRoleMapper;
import com.board.dao.RoleMapper;
import com.board.dto.RoleDto;
import com.board.entity.AccountRole;
import com.board.entity.Role;
import com.board.exception.RoleException;
import com.board.param.RoleListParam;
import com.board.service.RoleService;
import com.board.util.PageInfo;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.RoleNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AccountRoleMapper accountRoleMapper;

    @Override
    public PageInfo<RoleDto> loadAllRoles(RoleListParam param) {
        //查询所有角色及该角色所有的账号
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        Page<Role> page = new Page<>(param.getPageNum()==null?1: param.getPageNum(), param.getPageSize()==null?5: param.getPageSize());
        if(param.getRoleName()!=null){//带了查询条件
            wrapper.like("role_name",param.getRoleName());
        }
        roleMapper.selectPage(page,wrapper);
        ArrayList<RoleDto> roleDtos = new ArrayList<>();
        System.out.println(page.getRecords());
        for (Role role : page.getRecords()) {
            RoleDto dto = BeanUtil.toBean(role, RoleDto.class);
            roleDtos.add(dto);
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setList(roleDtos);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }

    @Override
    public Role addRole(String roleName) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",roleName);
        Role role = roleMapper.selectOne(wrapper);
        if(role!=null) throw new RoleException(500,"角色名已存在");
        //角色名不存在，可以新增
        Role addRole = new Role();
        addRole.setRoleName(roleName);
        roleMapper.insert(addRole);
        return addRole;
    }

    @Override
    public RoleDto findRoleById(Integer roleId) {
        Role role = roleMapper.selectById(roleId);
        if (role==null) throw new RoleException(500,"该角色不存在");
        RoleDto dto = BeanUtil.toBean(role, RoleDto.class);
        return dto;
    }

    @Override
    public void deleteRole(Integer roleId) {
        //查询删除的角色是否存在
        Role role = roleMapper.selectById(roleId);
        if (role==null) throw new RoleException(500,"该角色不存在");
        //如果有账号是该角色，不能删除
        QueryWrapper<AccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<AccountRole> list = accountRoleMapper.selectList(wrapper);
        if (list.size()>0) throw new RoleException(500,"该角色有账号，不能删除");
        //可以进行删除
        roleMapper.deleteById(roleId);
    }

    @Override
    public List<Role> findAllRole() {
        List<Role> list = roleMapper.selectList(new QueryWrapper<Role>().orderByAsc("role_id"));
        return list;
    }
}
